package com.victor.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.sql.visitor.functions.If;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.github.pagehelper.PageInfo;
import com.victor.entity.Administrator;
import com.victor.entity.JingqubusinessShop;
import com.victor.entity.JingqubusinessShopclass;
import com.victor.entity.Jingqubusinessshop_photo;
import com.victor.entity.Jingqulist;
import com.victor.entity.Parameter;
import com.victor.entity.Specifications;
import com.victor.service.JbshopService;
import com.victor.service.JingquService;
import com.victor.utils.Msg;

@RestController
@RequestMapping("jbshop")
public class JbshopController {
	@Autowired
	private JbshopService jbshopService;
	@Autowired
	private JingquService jingquService;
	@Autowired
	private LogController logcontroller;
	
	
	/**
	 *景区独立商务  列表
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/all")
	public Msg getJingquList(int page,int row,String adminid) {
		PageInfo<Jingqulist> pageInfo = jingquService.getJingquListAll(page,row);
		logcontroller.addLog(adminid, "查看了景区列表");
		return Msg.success().add("info", pageInfo);
	}
	/**
	 * 筛选 搜索（名称，类型，账号）
	 * @param province
	 * @param city
	 * @param search
	 * @return
	 */
	@RequestMapping("/shaixuan")
	public Msg choose(String province,String city,String search,String adminid,int page,int row) {
		HashMap<Object,Object> map = new HashMap<>();
		if (province != null && !province.isEmpty()) {
			map.put("province", province);
		}
		if (city != null && !city.isEmpty()) {
			map.put("city", city);
		}
		if (search != null && !search.isEmpty()) {
			map.put("search", search);
		}
		PageInfo<Jingqulist> list = jingquService.selByCondition(map, page, row);
		logcontroller.addLog(adminid, "搜索筛选了景区");
		return Msg.success().add("list", list);
	}
	/**
	 * 删除独立景区平台
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deletelist")
	public Msg deleteList(int[] ids,String adminid) {
		if (ids == null || ids.length<=0) {
			return Msg.fail("入参错误");
		}
		int deleteList = jingquService.deleteList(ids);
		if (deleteList<=0) {
			return Msg.fail("删除失败");
		}
		logcontroller.addLog(adminid, "删除了景区列表");
		return Msg.success();
	}
	
	/**
	 * 导出景区
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/export")
	public void export(HttpServletRequest request,HttpServletResponse response,String adminid) throws Exception {
		List<Jingqulist> list = jingquService.getAllListNopageandrow();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("景区列表");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short)0, (short)12);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("景区列表");
		
		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("景区名称");
		headrow.createCell(1).setCellValue("景区价格");
		headrow.createCell(2).setCellValue("景区活动价");
		headrow.createCell(3).setCellValue("景区级别");
		headrow.createCell(4).setCellValue("景区简介");
		headrow.createCell(5).setCellValue("景区类型");
		headrow.createCell(6).setCellValue("景区定位经度");
		headrow.createCell(7).setCellValue("景区定位纬度");
		headrow.createCell(8).setCellValue("省");
		headrow.createCell(9).setCellValue("市");
		headrow.createCell(10).setCellValue("创建时间");
		headrow.createCell(11).setCellValue("账号");
		headrow.createCell(12).setCellValue("密码");
		
		for(Jingqulist jingqulist : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			if(jingqulist.getJqname()==null) {
				dataRow.createCell(0).setCellValue("");
			}else {
				dataRow.createCell(0).setCellValue(jingqulist.getJqname());
			}
			
			if(jingqulist.getJqprice() == null) {
				dataRow.createCell(1).setCellValue("");
			}else {
				dataRow.createCell(1).setCellValue(jingqulist.getJqprice());
			}
			if(jingqulist.getJqhdprice() == null) {
				dataRow.createCell(2).setCellValue("");
			}else {
				dataRow.createCell(2).setCellValue(jingqulist.getJqhdprice());
			}
			if(jingqulist.getJqlevel()== null) {
				dataRow.createCell(3).setCellValue("");
			}else {
				dataRow.createCell(3).setCellValue(jingqulist.getJqlevel());
			}
			if(jingqulist.getJqjianjie() == null) {
				dataRow.createCell(4).setCellValue("");
			}else {
				dataRow.createCell(4).setCellValue(jingqulist.getJqjianjie());
			}
			if(jingqulist.getJqclass()== null) {
				dataRow.createCell(5).setCellValue("");
			}else {
				dataRow.createCell(5).setCellValue(jingqulist.getJqclass());
			}
			if(jingqulist.getJqlocation_lon() == null) {
				dataRow.createCell(6).setCellValue("");
			}else {
				dataRow.createCell(6).setCellValue(jingqulist.getJqlocation_lon());
			}
			if(jingqulist.getJqlocation_lat() == null) {
				dataRow.createCell(7).setCellValue("");
			}else {
				dataRow.createCell(7).setCellValue(jingqulist.getJqlocation_lat());
			}
			if(jingqulist.getProvince() == null) {
				dataRow.createCell(8).setCellValue("");
			}else {
				dataRow.createCell(8).setCellValue(jingqulist.getProvince());
			}
			if(jingqulist.getCity() == null) {
				dataRow.createCell(9).setCellValue("");
			}else {
				dataRow.createCell(9).setCellValue(jingqulist.getCity());
			}
			if(jingqulist.getJqtime() == null) {
				dataRow.createCell(10).setCellValue("");
			}else {
				dataRow.createCell(10).setCellValue(jingqulist.getJqtime());
			}
			if(jingqulist.getUsername() == null) {
				dataRow.createCell(11).setCellValue("");
			}else {
				dataRow.createCell(11).setCellValue(jingqulist.getUsername());
			}
			if(jingqulist.getPassword() == null) {
				dataRow.createCell(12).setCellValue("");
			}else {
				dataRow.createCell(12).setCellValue(jingqulist.getPassword());
			}
		}
		String filename = "景区列表";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
		logcontroller.addLog(adminid, "导出景区列表");
	}
	
 	/**
	 * 商品列表
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/getlist")
	public Msg findAll(int page,int row,Integer jbid,String adminid) {
		if (jbid == null) {
			return Msg.fail("入参错误");
		}
		PageInfo<JingqubusinessShop> pageInfo = jbshopService.selAll(page,row,jbid);
		logcontroller.addLog(adminid, "查看了商品列表");
		return Msg.success().add("info", pageInfo);
		
	}
	
	/**
	 * 下架 上架
	 * 
	 * @return
	 */
	@RequestMapping("/sxstate")
	public Msg xjState(JingqubusinessShop jingqubusinessShop,String adminid) {
		if (jingqubusinessShop.getSxflag() == null || jingqubusinessShop.getSxflag().isEmpty()) {
			return Msg.fail("入参错误");
		}
		if (jingqubusinessShop.getSxflag().equals("1")) {
			jingqubusinessShop.setSxflag("1");
			boolean b = jbshopService.update(jingqubusinessShop);
			if (!b) {
				return Msg.fail("修改错误");
			}
		}
		if (jingqubusinessShop.getSxflag().equals("0")) {
			jingqubusinessShop.setSxflag("0");
			boolean b = jbshopService.update(jingqubusinessShop);
			if (!b) {
				return Msg.fail("修改错误");
			}
		}
		logcontroller.addLog(adminid, "修改上下架状态");
		return Msg.success();
	}
	/**
	 * 查询商品详情( TODO 有错)
	 * @param id
	 * @return
	 */
	@RequestMapping("/getshop")
	public Msg findShopById(Integer id,String adminid) {
		if (id == null || adminid.isEmpty() ) {
			return Msg.fail("入参失败");
		}
		JingqubusinessShop jingqubusinessShop =  jbshopService.selShopById(id);
		logcontroller.addLog(adminid, "查看了商品详情");
		return Msg.success().add("shop", jingqubusinessShop);
	}
	/**
	 * 根据条件查询查询
	 * @param search
	 * @param sxflag
	 * @param tjflag
	 * @param adminid
	 * @return
	 */
	@RequestMapping("/condition")
	public Msg searchAndXialakuang(String search,String sxflag,String tjflag,String adminid,String classid) {
		HashMap<Object,Object> hashMap = new HashMap<>();
		if (search != null && !search.isEmpty()) {
			hashMap.put("search", search);
		}
		if (sxflag != null && !sxflag.isEmpty()) {
			hashMap.put("sxflag", sxflag);
		}
		if (tjflag != null && !tjflag.isEmpty()) {
			hashMap.put("tjflag", tjflag);
		}
		if (classid != null && !classid.isEmpty()) {
			hashMap.put("shopclassid", classid);
		}
		List<JingqubusinessShop> list = jbshopService.selByCondition(hashMap);
		logcontroller.addLog(adminid, "根据条件查询了商品");
		return Msg.success().add("list", list);
	}
	/**
	 * 查询全部分类
	 * @param page
	 * @param row
	 * @param adminid
	 * @return
	 */
	@RequestMapping("/getshopclass")
	public Msg shopclass(int page,int row,String adminid) {
		if (adminid== null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<JingqubusinessShopclass> pageInfo = jbshopService.selShopclass(page,row);
		logcontroller.addLog(adminid, "查看了商品的分类");
		return Msg.success().add("info", pageInfo);
	}
	/**
	 * 上传商品
	 * @param shop
	 * @return
	 */
	@RequestMapping("/addshop")
	public Msg addShop(@RequestBody JingqubusinessShop shop) {
		if (shop == null || shop.getJlid()==null || shop.getJlid().isEmpty() || shop.getAdminid()==null || shop.getAdminid().isEmpty() ) {
			return Msg.fail("入参错误");
		}
		boolean b = jbshopService.insert(shop);
		if (!b) {
			return Msg.fail("添加商品错误");
		}
		if (shop.getPhotos()!= null && shop.getPhotos().size()>0) {
			for (int i = 0; i < shop.getPhotos().size(); i++) {
				shop.getPhotos().get(i).setJbid(String.valueOf(shop.getId()));
				boolean shopphoto = jbshopService.insertShopphoto(shop.getPhotos().get(i));
				if (!shopphoto) {
					return Msg.fail("添加图片失败");
				}
			}
		}
		if (shop.getList() != null && !shop.getList().isEmpty()) {
			for (int i = 0; i < shop.getList().size(); i++) {
				shop.getList().get(i).setSid(String.valueOf(shop.getId()));
				 boolean guige = jbshopService.insertGuige(shop.getList().get(i));
				 if (!guige) {
					return Msg.fail("添加规格失败");
				}
				 if (shop.getList().get(i).getList() != null && shop.getList().get(i).getList().size()>0) {
					for (int j = 0; j < shop.getList().get(i).getList().size(); j++) {
						shop.getList().get(i).getList().get(j).setSpid(String.valueOf(shop.getList().get(i).getId()));
						boolean parameter = jbshopService.insertParameter(shop.getList().get(i).getList().get(j));
						if (!parameter) {
							return Msg.fail("添加参数失败");
						}
					}
				}
			}
		}
		logcontroller.addLog(shop.getAdminid(), "添加了商品为"+shop.getId());
		return Msg.success();
		
	}
	/**
	 * 添加商品图片
	 * @param photo
	 * @return
	 */
	@RequestMapping("/addshopphoto")
	public Msg addShopphoto(Jingqubusinessshop_photo photo,String adminid) {
		if (photo== null || photo.getJbid()==null || photo.getJbid().isEmpty() || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = jbshopService.insertShopphoto(photo);
		if (!b) {
			return Msg.fail("添加图片失败");
		}
		logcontroller.addLog(adminid, "添加了"+photo.getJbid()+"商品的图片");
		return Msg.success();
		
	}
	/**
	 * 添加规格
	 * @param specifications
	 * @return
	 */
	@RequestMapping("/addguige")
	public Msg addGuiGe(Specifications specifications,String adminid) {
		if (specifications == null || specifications.getSid()==null || specifications.getSid().isEmpty() || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = jbshopService.insertGuige(specifications);
		if (!b) {
			return Msg.fail("添加规格失败");
		}
		logcontroller.addLog(adminid, "添加了"+specifications.getSid()+"商品的规格");
		return Msg.success();
	}
	/**
	 * 添加参数
	 * @param parameter
	 * @return
	 */
	@RequestMapping("/addparameter")
	public Msg addParameter(Parameter parameter,String adminid) {
		if (parameter == null || parameter.getSpid() == null || parameter.getSpid().isEmpty() || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = jbshopService.insertParameter(parameter);
		if (!b) {
			return Msg.fail("添加参数失败");
		}
		logcontroller.addLog(adminid, "添加了"+parameter.getSpid()+"规格的参数");
		return Msg.success();
	}
	/**
	 * 修改商品
	 * @param shop
	 * @return
	 */
	@RequestMapping("/updateshop")
	public Msg updateShop(JingqubusinessShop shop,String adminid) {
		if (shop == null || shop.getId()== null) {
			return Msg.fail("入参失败");
		}
		boolean b = jbshopService.update(shop);
		if (!b) {
			return Msg.fail("修改失败");
		}
		logcontroller.addLog(adminid, "修改了"+shop.getId()+"商品");
		return Msg.success();
	}
	/**
	 * 修改商品图片
	 * @param photo
	 * @return
	 */
	@RequestMapping("/updshopphoto")
	public Msg updateShopPhoto(Jingqubusinessshop_photo photo,String adminid) {
		if (photo == null || photo.getId()== null || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = jbshopService.updShopPhoto(photo);
		if (!b) {
			return Msg.fail("修改商品图片失败");
		}
		logcontroller.addLog(adminid, "修改了商品"+photo.getJbid()+"的图片");
		return Msg.success();
	}
	/**
	 * 修改商品规格
	 * @param specifications
	 * @return
	 */
	@RequestMapping("/updguige")
	public Msg updateGuige(Specifications specifications,String adminid) {
		if (specifications == null || specifications.getId()== null || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = jbshopService.updGuige(specifications);
		if (!b) {
			return Msg.fail("修改规格失败");
		}
		logcontroller.addLog(adminid, "修改了商品"+specifications.getSid()+"的规格");
		return Msg.success();
	}
	/**
	 * 修改参数
	 * @param parameter
	 * @return
	 */
	@RequestMapping("/updateparameter")
	public Msg updateParameter(Parameter parameter,String adminid) {
		if (parameter == null || parameter.getId()== null || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean updParameter = jbshopService.updParameter(parameter);
		if (!updParameter) {
			return Msg.fail("修改参数错误");
		}
		logcontroller.addLog(adminid, "修改了规格"+parameter.getSpid()+"的参数");
		return Msg.success();
	}
	/**
	 * 删除商品
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteshop")
	public Msg deleteShop(int[] ids,String adminid) {
		if (ids == null || ids.length<=0 || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = jbshopService.delShop(ids);
		if (!b) {
			return Msg.fail("删除商品错误");
		}
		logcontroller.addLog(adminid, "删除了商品"+ids);
		return Msg.success();
	}

}
